package com.yytek.nmp.controller;

import java.util.List;
import java.io.IOException;

import com.yytek.nmp.domain.IotDeviceInfo;
import com.yytek.nmp.service.IIotDeviceInfoService;
import com.yytek.nmp.service.impl.IotDeviceInfoServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yytek.common.log.annotation.Log;
import com.yytek.common.log.enums.BusinessType;
import com.yytek.common.security.annotation.RequiresPermissions;
import com.yytek.nmp.domain.IotDeviceGroup;
import com.yytek.nmp.service.IIotDeviceGroupService;
import com.yytek.common.core.web.controller.BaseController;
import com.yytek.common.core.web.domain.AjaxResult;
import com.yytek.common.core.utils.poi.ExcelUtil;
import com.yytek.common.core.web.page.TableDataInfo;

/**
 * 设备分组Controller
 * 
 * @author yytek-iot
 * @date 2022-07-05
 */
@Api(value = "设备分组控制器", tags = {"设备分组管理"})
@RestController
@RequestMapping("/group")
public class IotDeviceGroupController extends BaseController
{
    @Autowired
    private IIotDeviceGroupService iotDeviceGroupService;
    @Autowired
    private IIotDeviceInfoService iotDeviceInfoService;
    /**
     * 分页查询分组关联设备管理列表
     */
    @ApiOperation("分页查询分组关联设备管理列表")
    @GetMapping("/bindDeviceList")
    public TableDataInfo bindDeviceList(IotDeviceInfo iotDeviceInfo)
    {
        startPage();
        List<IotDeviceInfo> list = iotDeviceInfoService.selectIotDeviceInfoList(iotDeviceInfo);
        return getDataTable(list);
    }

    /**
     * 分页查询未关联设备分组的设备列表
     * @return
     */
    @ApiOperation("分页查询未关联设备分组的设备列表")
    @GetMapping("/noBindDeviceList")
    public TableDataInfo noBindDeviceList(String projectId)
    {
        startPage();
        List<IotDeviceInfo> list = iotDeviceGroupService.noBindDeviceList(projectId);
        return getDataTable(list);
    }

    /**
     * 分页查询设备分组列表
     */
    @ApiOperation("分页查询设备分组列表")
    @RequiresPermissions("nmp:group:list")
    @GetMapping("/list")
    public TableDataInfo list(IotDeviceGroup iotDeviceGroup)
    {
        startPage();
        List<IotDeviceGroup> list = iotDeviceGroupService.selectIotDeviceGroupList(iotDeviceGroup);
        return getDataTable(list);
    }

    /**
     * 导出设备分组列表
     */
    @ApiOperation("导出设备分组列表")
    @RequiresPermissions("nmp:group:export")
    @Log(title = "设备分组", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, IotDeviceGroup iotDeviceGroup)
    {
        List<IotDeviceGroup> list = iotDeviceGroupService.selectIotDeviceGroupList(iotDeviceGroup);
        ExcelUtil<IotDeviceGroup> util = new ExcelUtil<IotDeviceGroup>(IotDeviceGroup.class);
        util.exportExcel(response, list, "设备分组数据");
    }

    /**
     * 获取设备分组详细信息
     */
    @ApiOperation("获取设备分组详细信息")
    @RequiresPermissions("nmp:group:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(iotDeviceGroupService.selectIotDeviceGroupById(id));
    }

    /**
     * 新增设备分组
     */
    @ApiOperation("新增设备分组")
    @RequiresPermissions("nmp:group:add")
    @Log(title = "设备分组", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody IotDeviceGroup iotDeviceGroup)
    {
        return  iotDeviceGroupService.insertIotDeviceGroup(iotDeviceGroup);
    }

    /**
     * 修改设备分组
     */
    @ApiOperation("修改设备分组")
    @RequiresPermissions("nmp:group:edit")
    @Log(title = "设备分组", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody IotDeviceGroup iotDeviceGroup)
    {
        return iotDeviceGroupService.updateIotDeviceGroup(iotDeviceGroup);
    }

    /**
     * 删除设备分组
     */
    @ApiOperation("删除设备分组")
    @RequiresPermissions("nmp:group:remove")
    @Log(title = "设备分组", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(iotDeviceGroupService.deleteIotDeviceGroupByIds(ids));
    }
}
